from Tkinter import *
import icons
import toolpaletteprops
import mutil

class ToolPalette():
	title = 'Tools'
	currentTool = 'design'
	appobj=None
	def __init__(self, appobj):
		self.appobj = appobj
	def launch(self, root):
		self.windowRoot = Toplevel(root)
		self.windowRoot.title(self.title)
		self.windowRoot.iconname(self.title)
		self.windowRoot.resizable(0,0)
		self.windowRoot.wm_attributes("-toolwindow", 1)
		frNotebook = Frame(self.windowRoot)
		frNotebook.pack(expand=TRUE, fill=BOTH)
		self.nb = TkNotebook(frNotebook, TOP)

		frameDesign = Frame(self.nb())
		for name,o in self.appobj.supportedObjs:
			b=Button(frameDesign, text='Add '+name, command=mutil.Callable(self.appobj.createNewWidgetFromPalette,o))
			b.pack(side=TOP, padx=2, pady=2)
		
		frameProps = Frame(self.nb())
		self.frames = toolpaletteprops.createFrames(self.appobj, frameProps)
		self.frameProps = frameProps
		
		frameTest = Frame(self.nb())
		b1 = Button(frameTest, text='Generate code.',command=self.onGenCode)
		self.lblSaveStatus = Label(frameTest, text='')
		b1.pack(fill=BOTH, expand=1)
		self.lblSaveStatus.pack(fill=BOTH, expand=1)
		
		self.btnDesign = self.nb.add_screen(frameDesign, "Design")
		self.btnProps = self.nb.add_screen(frameProps, "Props")
		self.btnTest = self.nb.add_screen(frameTest, "Test")
		icons.addIconToWidget(self.btnDesign, icons.iconPen)
		icons.addIconToWidget(self.btnProps, icons.iconDots)
		icons.addIconToWidget(self.btnTest, icons.iconHand)
		self.nb.callup = self.refreshFrame  #todo: hack to refresh frame
	
	def openFirstTab(self):
		self.btnDesign.invoke()
	
	def refreshFrame(self):
		self.lblSaveStatus.config(text='')
		if self.nb.active_fr==self.frameProps: 
			
			shellobject = self.appobj.getSelected()
			whichFrame= self.frames[shellobject.pyrindClassName]
			for fr in self.frames:
				self.frames[fr].forget()
			whichFrame.pack(fill=BOTH, expand=1)
			
			for prop in whichFrame._propObjects:
				prop.updateInfo()
				
	def onGenCode(self):
		s = self.appobj.runProject()
		self.lblSaveStatus.config(text='Saved to '+s+'.')
		
		

# file: notebook.py. A simple notebook-like Tkinter widget.
# Copyright 2003, Iuri Wickert (iwickert yahoo.com)
class TkNotebook():
	callup=None
	# initialization. receives the master widget
	# reference and the notebook orientation
	def __init__(self, master, side=LEFT):
		self.active_fr = None
		self.count = 0
		self.choice = IntVar(0)
		# allows the TOP and BOTTOM
		# radiobuttons' positioning.
		if side in (TOP, BOTTOM):
			self.side = LEFT
		else:
			self.side = TOP
		# creates notebook's frames structure
		self.rb_fr = Frame(master, borderwidth=2, relief=RIDGE)
		self.rb_fr.pack(side=side, fill=BOTH)
		self.screen_fr = Frame(master, borderwidth=2, relief=RIDGE)
		self.screen_fr.pack(fill=BOTH)

	# return a master frame reference for the external frames (screens)
	def __call__(self):
		return self.screen_fr
		
	# add a new frame (screen) to the (bottom/left of the) notebook
	def add_screen(self, fr, title):
		b = Radiobutton(self.rb_fr, text=title, indicatoron=0, \
			variable=self.choice, value=self.count, \
			command=lambda: self.display(fr))
		b.pack(fill=BOTH, side=self.side)
		
		# ensures the first frame will be
		# the first selected/enabled
		if not self.active_fr:
			fr.pack(fill=BOTH, expand=1)
			self.active_fr = fr

		self.count += 1
		# returns a reference to the newly created radiobutton (allowing its configuration/destruction)         
		return b

	# hides the former active frame and shows 
	# another one, keeping its reference
	def display(self, fr):
		self.active_fr.forget()
		fr.pack(fill=BOTH, expand=1)
		self.active_fr = fr
		self.callup()

if __name__=='__main__':
	root = Tk()
	def onTest(root=root):
		se = ToolPalette()
		se.launch(root)
		#se.draw_form()
	t = Button(root, text='Test', command=onTest)
	t.pack()
	t.mainloop()

